package carassius.DAL;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author siebz0r
 */
public class PlantRow
		extends OrganismRow
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private int _light;
	private final String SQL_UPDATE_LIGHT = "UPDATE plant SET light = ? where id = ?";
	private final Object LIGHT_LOCK;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public PlantRow(ResultSet resultSet) throws SQLException
	{
		super(resultSet);
		_light = resultSet.getInt("light");
		LIGHT_LOCK = new Object();
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Properties">
	public int getLight()
	{
		synchronized (LIGHT_LOCK)
		{
			return _light;
		}
	}

	public boolean setLight(int light)
	{
		boolean result = false;
		synchronized (LIGHT_LOCK)
		{
			if (light != _light)
			{
				try
				{
					PreparedStatement st = Database.prepareStatement(SQL_UPDATE_LIGHT);
					st.setInt(1, light);
					st.setInt(2, getId());

					if (st.executeUpdate() == 1)
					{
						_light = light;
						result = true;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	@Override
	public boolean equals(Object obj)
	{
		if (!super.equals(obj))
		{
			return false;
		}
		if (obj == null)
		{
			return false;
		}
		if (getClass() != obj.getClass())
		{
			return false;
		}
		final PlantRow other = (PlantRow) obj;
		if (this._light != other._light)
		{
			return false;
		}
		return true;
	}

	@Override
	public int hashCode()
	{
		int hash = 3;
		return hash;
	}
	//</editor-fold>
}
